/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package assisisw.smartcal.webuisubsystem.managedbean.validator;

import java.io.Serializable;
import java.util.Objects;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

/**
 *
 * @author Francesco
 */
@ManagedBean ( name = "lastNameValidator" )
public class LastNameValidator implements Validator , Serializable
{

     // Static Fields
    
    /***/
    private static final FacesMessage sizeTooLongMessage = new FacesMessage ( "Sorry , name too long !" ) ;
    
    /***/
    private static final FacesMessage lastNameRequiredMessage = new FacesMessage ( "Sorry, last name required !" ) ;
    
    /***/
    private static final int MAX_SIZE = 15 ;
    
    // Instance Methods
    
    /***/
    public LastNameValidator () {}
    
    /**
     * AS THE SUPER'S ONE.
     */
    @Override
    public void validate ( FacesContext context , UIComponent component , Object value) 
            throws ValidatorException 
    {
        FacesMessage errorMessage ;
        String uN ;
        boolean lastNameValid ;
        errorMessage = null ;
        if ( value != null && ! value.toString().isEmpty() )
        {
            uN = value.toString();
            if ( uN.length () > MAX_SIZE )
            {
                errorMessage = sizeTooLongMessage ;
                lastNameValid = false ;
            }
            else
                lastNameValid = true ;
        }
        else
        {
            errorMessage = lastNameRequiredMessage ;
            lastNameValid = false ;
        }
        if ( Objects.nonNull ( errorMessage ) )
            throw new ValidatorException ( errorMessage ) ;
    }
    
}
